<?php
$total = $total ?? 0;
$currentPage = $currentPage ?? 1;
$perPage = $perPage ?? 8;
$totalPages = $totalPages ?? 1;
$categoryId = $categoryId?? '';
?>

<?php if ($total > 0): ?>
    <div class="mt-6 flex justify-center gap-2 items-center flex-wrap">
        <!-- 上一页 -->
        <?php if ($currentPage > 1): ?>
            <a href="?page=<?= $currentPage - 1 ?>&per_page=<?= $perPage ?>&q=<?= htmlspecialchars(urlencode($_GET['q'] ?? '')) ?>" class="px-3 py-1 border rounded hover:bg-gray-100">上一页</a>
        <?php endif; ?>

        <!-- 页码列表 -->
        <?php for ($i = max(1, $currentPage - 2); $i <= min($totalPages, $currentPage + 2); $i++): ?>
            <a
                href="?page=<?= $i ?>&per_page=<?= $perPage ?>&q=<?= htmlspecialchars(urlencode($_GET['q'] ?? '')) ?>"
                class="px-3 py-1 border rounded <?= $i === $currentPage ? 'bg-blue-500 text-white' : 'hover:bg-gray-100' ?>"
            >
                <?= $i ?>
            </a>
        <?php endfor; ?>

        <!-- 下一页 -->
        <?php if ($currentPage < $totalPages): ?>
            <a href="?page=<?= $currentPage + 1 ?>&per_page=<?= $perPage ?>&q=<?= htmlspecialchars(urlencode($_GET['q'] ?? '')) ?>&category_id=<?= $categoryId?>" class="px-3 py-1 border rounded hover:bg-gray-100">下一页</a>
        <?php endif; ?>

        <!-- 每页显示输入框 -->
        <form method="get" class="flex items-center gap-2">
            <label>每页显示：</label>
            <input
                type="hidden"
                name="category_id"
                value="<?= $categoryId?>"
            >
            <input
                type="number"
                name="per_page"
                value="<?= htmlspecialchars($perPage) ?>"
                min="1"
                max="100"
                class="border p-1 rounded"
            >
            <label>条</label>
            <button type="submit" class="px-3 py-1 bg-blue-500 text-white rounded-md">设置</button>
        </form>
    </div>
<?php endif; ?>
